package com.yy.controller.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.context.BaseContext;
import com.yy.controller.mapper.DishMapper;
import com.yy.controller.mapper.SetmealDishMapper;
import com.yy.controller.service.DishService;
import com.yy.dto.DishPageQueryDTO;
import com.yy.entity.Dish;
import com.yy.entity.SetmealDish;
import com.yy.exception.BaseException;
import com.yy.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询菜品信息
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(DishPageQueryDTO dishPageQueryDTO) {
        //开始分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishMapper.pageList(dishPageQueryDTO);
        log.info("分页查询:{}", page);
        long total = page.getTotal();
        List<Dish> records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 新增菜品
     *
     * @param dish
     */
    @Override
    @Transactional
    public void add(Dish dish) {
        Long userId = BaseContext.getCurrentId();
        dish.setCreateUser(userId);
        dish.setUpdateUser(userId);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dishMapper.add(dish);
    }

    /**
     * 修改菜品
     *
     * @param dish
     */
    @Override
    public void update(Dish dish) {
        Long userId = BaseContext.getCurrentId();
        dish.setUpdateUser(userId);
        dish.setUpdateTime(LocalDateTime.now());
        dishMapper.update(dish);
    }

    /**
     * 根据id删除菜品
     *
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        //是否关联的套餐
        Integer num = setmealDishMapper.getNumByDishId(id);
        if (num > 0) {
            throw new BaseException("该菜品关联了套餐不能删除!");
        }
        //是否起售中
        Dish dish = dishMapper.getById(id);
        if (dish.getStatus() == 1) {
            throw new BaseException("该菜品起售中不能删除!");
        }
        dishMapper.delete(id);
    }

    /**
     * 获取所有菜品
     *
     * @return
     */
    @Override
    public List<Dish> getAllDish() {
        List<Dish> dishList = dishMapper.getAllDish();
        return dishList;
    }

    /**
     * 根据ids数组查询菜品
     *
     * @param ids
     * @return
     */
    @Override
    public List<SetmealDish> getDishByIds(String ids) {
        List<Dish> dishList = new ArrayList<>();
        String[] strArray = ids.split(",");
        Long[] longArray = new Long[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            longArray[i] = Long.parseLong(strArray[i]);
        }
        List<SetmealDish> setmealDishList = new ArrayList<>();

        for (Long id : longArray) {
            SetmealDish setmealDish = new SetmealDish();
            Dish dish = dishMapper.getById(id);
            setmealDish.setName(dish.getName());
            setmealDish.setPrice(dish.getPrice());
            setmealDish.setDishId(dish.getId());
            setmealDish.setCopies(1);
            setmealDishList.add(setmealDish);
        }
        return setmealDishList;
    }
}
